package com.shupian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shupian.constants.SystemConstants;
import com.shupian.domain.ResponseResult;
import com.shupian.domain.dto.CategoryAddDto;
import com.shupian.domain.entity.Article;
import com.shupian.domain.entity.Category;
import com.shupian.domain.vo.CategoryAllVo;
import com.shupian.domain.vo.CategoryPageVo;
import com.shupian.domain.vo.CategoryVo;
import com.shupian.domain.vo.PageVo;
import com.shupian.enums.AppHttpCodeEnum;
import com.shupian.mapper.CategoryMapper;
import com.shupian.service.ArticleService;
import com.shupian.service.CategoryService;
import com.shupian.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2022-05-17 21:13:14
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private ArticleService articleService;

    @Override
    public ResponseResult getCategoryList() {
        //查询文章表 状态为已发布的文章
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> list = articleService.list(articleWrapper);
        //获取文章的分类id ，并且去重
        Set<Long> collect = list.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());
        //查询分类表
        List<Category> categories = listByIds(collect);
        categories.stream()
                .filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //封装vo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult getAllCategory() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus,SystemConstants.CATEGORY_STATUS_NORMAL);
        List<Category> categoryList = list(queryWrapper);
        List<CategoryAllVo> categoryAllVos = BeanCopyUtils.copyBeanList(categoryList, CategoryAllVo.class);
        return ResponseResult.okResult(categoryAllVos);
    }

    @Override
    public ResponseResult getPageList(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),Category::getName,name);
        queryWrapper.eq(StringUtils.hasText(status),Category::getStatus,status);
        Page<Category> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Category> categories = page.getRecords();
        List<CategoryPageVo> categoryPageVos = BeanCopyUtils.copyBeanList(categories, CategoryPageVo.class);

        return ResponseResult.okResult(new PageVo(categoryPageVos,page.getTotal()));
    }

    @Override
    public ResponseResult addCategory(CategoryAddDto categoryAddDto) {
        Category category = BeanCopyUtils.copyBean(categoryAddDto, Category.class);
        return save(category) ? ResponseResult.okResult():ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult updateCategory(CategoryPageVo categoryPageVo) {
        Long id = categoryPageVo.getId();
        Category category = getById(id);
        category.setName(categoryPageVo.getName());
        category.setDescription(categoryPageVo.getDescription());
        category.setStatus(categoryPageVo.getStatus());
        return updateById(category)? ResponseResult.okResult():ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

}

